package com.filex.ads.common;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;

import com.filex.ads.common.logging.Logger;
import com.filex.ads.common.utils.Intents;

import static com.filex.ads.common.Constants.HTTP;
import static com.filex.ads.common.Constants.HTTPS;


public enum UrlAction {

    HANDLE_PHONE_SCHEME(true) {
        @Override
        public boolean shouldTryHandlingUrl(@NonNull final Uri uri) {
            final String scheme = uri.getScheme();
            return "tel".equalsIgnoreCase(scheme) || "voicemail".equalsIgnoreCase(scheme)
                    || "sms".equalsIgnoreCase(scheme) || "mailto".equalsIgnoreCase(scheme)
                    || "geo".equalsIgnoreCase(scheme)
                    || "google.streetview".equalsIgnoreCase(scheme);
        }

        @Override
        protected void performAction(
                @NonNull final Context context, @NonNull final Uri uri,
                @NonNull final UrlHandler urlHandler)
                throws IntentNotResolvableException {
            final String errorMessage = "Could not handle intent with URI: " + uri + "\n\tIs " +
                    "this intent supported on your phone?";
            Intents.launchActionViewIntent(context, uri, errorMessage);
        }
    },

    OPEN_NATIVE_BROWSER(true) {
        @Override
        public boolean shouldTryHandlingUrl(@NonNull final Uri uri) {
            final String scheme = uri.getScheme();
            return HTTP.equalsIgnoreCase(scheme) || HTTPS.equalsIgnoreCase(scheme);

        }

        @Override
        protected void performAction(
                @NonNull final Context context, @NonNull final Uri uri,
                @NonNull final UrlHandler urlHandler)
                throws IntentNotResolvableException {
            final String errorMessage = "Unable to load native browser url: " + uri;
            final Intent intent = Intents.intentForNativeBrowserScheme(uri);
            Intents.launchIntentForUserClick(context, intent, errorMessage);
        }
    },

    OPEN_APP_MARKET(true) {
        @Override
        public boolean shouldTryHandlingUrl(@NonNull final Uri uri) {
            final String scheme = uri.getScheme();
            final String host = uri.getHost();

            return "play.google.com".equalsIgnoreCase(host)
                    || "market.android.com".equalsIgnoreCase(host)
                    || "market".equalsIgnoreCase(scheme)
                    || uri.toString().toLowerCase().startsWith("play.google.com/")
                    || uri.toString().toLowerCase().startsWith("market.android.com/");
        }

        @Override
        protected void performAction(
                @NonNull final Context context, @NonNull final Uri uri,
                @NonNull final UrlHandler urlHandler)
                throws IntentNotResolvableException {
            Intents.launchApplicationUrl(context, uri);
        }
    },

    /* This is essentially an "unspecified" value for UrlAction. */
    NOOP(false) {
        @Override
        public boolean shouldTryHandlingUrl(@NonNull final Uri uri) {
            return false;
        }

        @Override
        protected void performAction(
                @NonNull final Context context, @NonNull final Uri uri,
                @NonNull final UrlHandler urlHandler) {
        }
    };

    private final boolean mRequiresUserInteraction;

    UrlAction(boolean requiresUserInteraction) {
        mRequiresUserInteraction = requiresUserInteraction;
    }

    public void handleUrl(
            UrlHandler urlHandler,
            @NonNull final Context context,
            @NonNull final Uri destinationUri,
            final boolean fromUserInteraction)
            throws IntentNotResolvableException {
        Logger.d("Ad event URL: " + destinationUri);
        if (mRequiresUserInteraction && !fromUserInteraction) {
            throw new IntentNotResolvableException("Attempted to handle action without user " +
                    "interaction.");
        } else {
            performAction(context, destinationUri, urlHandler);
        }
    }

    public abstract boolean shouldTryHandlingUrl(@NonNull final Uri uri);

    protected abstract void performAction(
            @NonNull final Context context, @NonNull final Uri uri,
            @NonNull final UrlHandler urlHandler)
            throws IntentNotResolvableException;
}
